package com.feihong.txgw.tx_model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.product.IProDetailContract;
import com.feihong.txgw.json.goodsCart.GoodsCartParser;
import com.feihong.txgw.model.bean.shopcar.ShopcarOperateEntity;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ProDetailModel
 * @package com.feihong.txgw.tx_model
 * @data 2018/5/8 12:00
 * @Describe
 * @Change
 */
public class ProDetailModel implements IProDetailContract.IProDetailMainModel {
    @Override
    public Observable<GoodsCartParser> addGoodsToShopCar(List<ShopCarGoodsModel> goods) {
        List<ShopcarOperateEntity> mKeyContent = new ArrayList<>();
        Map<String,String> mParams = new HashMap<>();
        if (goods != null && goods.size() > 0) {
            for (ShopCarGoodsModel item : goods) {
                if(!TextUtils.isEmpty(item.getAction())){
                    ShopcarOperateEntity shopcarOperateEntity = new ShopcarOperateEntity();
                    shopcarOperateEntity.setAction(item.getAction());
                    shopcarOperateEntity.setNums(String.valueOf(item.getGoodsNum()));
                    shopcarOperateEntity.setSku_id(String.valueOf(item.getGoodsID()));
                    mKeyContent.add(shopcarOperateEntity);
                }
            }
        }
        mParams.put("content",JSON.toJSONString(mKeyContent));
        return RetrofitManager.getInstance().editShopCarGoodsToService(mParams).compose(RxHelper.<GoodsCartParser>rxSchedulerHelper());
    }

    public static ProDetailModel create(){
        return new ProDetailModel();
    }
}
